import TagsAggregatorPlugin from "./main";
import {Setting} from "obsidian";

export class JobDef {
  tagText: string = "";
  scanRangePath: string = "";
  outputPath: string = "";
  enabled: boolean = true;
}

export class CommonSingleJobsUI {
  parent: HTMLElement;
  plugin: TagsAggregatorPlugin;

  constructor(parent: HTMLElement, plugin: TagsAggregatorPlugin) {
    this.parent = parent;
    this.plugin = plugin;
  }

  display() {
    const {parent} = this;

    /* Single Jobs Setting */
    parent.createEl("h2", { text: "Define Single Jobs" });
    const desc = document.createDocumentFragment();
    desc.append(
        "Define Single Jobs by tag, location, output and toggle enabled."
    );
    new Setting(parent).setDesc(desc);
    this.plugin.settings.jobs.forEach((job, index) => {
      const s = new Setting(parent)
          .addText(text => text
              .setPlaceholder("#经管")
              .setValue(this.plugin.settings.jobs[index].tagText)
              .onChange(async (value) => {
                    this.plugin.settings.jobs[index].tagText = value;
                    await this.plugin.saveSettings();
                  }
              ))
          .addText(text => text
              .setPlaceholder("1_Inbox/Diary/2023/2023-07")
              .setValue(this.plugin.settings.jobs[index].scanRangePath)
              .onChange(async (value) => {
                    this.plugin.settings.jobs[index].scanRangePath = value;
                    await this.plugin.saveSettings();
                  }
              ))
          .addText(text => text
              .setPlaceholder("1_Inbox/Diary/output.md")
              .setValue(this.plugin.settings.jobs[index].outputPath)
              .onChange(async (value) => {
                    this.plugin.settings.jobs[index].outputPath = value;
                    await this.plugin.saveSettings();
                  }
              ))
          .addToggle(toogle => toogle
              .setValue(this.plugin.settings.jobs[index].enabled)
              .onChange(async (value) => {
                    this.plugin.settings.jobs[index].enabled = value;
                    await this.plugin.saveSettings();
                  }
              ))
          // .addExtraButton((cb) => {
          //     cb.setIcon("up-chevron-glyph")
          //         .setTooltip("Move up")
          //         .onClick(() => {
          //             this.arraymove(
          //                 this.plugin.settings.jobs,
          //                 index,
          //                 index - 1
          //             );
          //             this.plugin.saveSettings();
          //             this.display();
          //         });
          // })
          // .addExtraButton((cb) => {
          //     cb.setIcon("down-chevron-glyph")
          //         .setTooltip("Move down")
          //         .onClick(() => {
          //             this.arraymove(
          //                 this.plugin.settings.jobs,
          //                 index,
          //                 index + 1
          //             );
          //             this.plugin.saveSettings();
          //             this.display();
          //         });
          // })
          .addExtraButton((cb) => {
            cb.setIcon("cross")
                .setTooltip("Delete")
                .onClick(() => {
                  this.plugin.settings.jobs.splice(
                      index,
                      1
                  );
                  this.plugin.saveSettings();
                  // Force refresh
                  this.display();
                });
          })
          .addExtraButton(cb => {
            cb.setIcon("copy")
                .setTooltip("Copy")
                .onClick(() =>{
                  let copiedJobDef = new JobDef();
                  copiedJobDef.tagText = this.plugin.settings.jobs[index].tagText;
                  copiedJobDef.scanRangePath = this.plugin.settings.jobs[index].scanRangePath;
                  copiedJobDef.outputPath = this.plugin.settings.jobs[index].outputPath;
                  copiedJobDef.enabled = true;
                  this.plugin.settings.jobs.push(copiedJobDef);
                  this.plugin.saveSettings();
                  // Force refresh
                  this.display();
                })
          })
          .addExtraButton(cb => {
            cb.setIcon("play-circle")
                .setTooltip("Execute")
                .onClick(async () =>{
                  await this.plugin.executeSingeJob(this.plugin.settings.jobs[index], true);
                  // const content = await this.plugin.mergeDailyNotesHeading(this.plugin.settings.jobs[index].headingText,
                  //     this.plugin.settings.jobs[index].scanRangePath,
                  //     this.plugin.settings.includeSubHeadings,
                  //     true);
                  // this.plugin.writeToOutputPath(this.plugin.settings.jobs[index].outputPath, this.plugin.settings.jobs[index].headingText, content);
                })
          });

    });

    new Setting(parent).addButton((cb) => {
      cb.setButtonText("Add new job")
          .setCta()
          .onClick(() => {
            this.plugin.settings.jobs.push(new JobDef());
            this.plugin.saveSettings();
            // Force refresh
            this.display();
          });
    });
    /* end */
  }
}

export class CommonBatchJobUI {
  parent: HTMLElement;
  plugin: TagsAggregatorPlugin;

  constructor(parent: HTMLElement, plugin: TagsAggregatorPlugin) {
    this.parent = parent;
    this.plugin = plugin;
  }

  display() {
    const { parent } = this;
    /* Batch Job Settings */
    parent.createEl("h2", { text: "Batch Job Settings" });
    const desc3 = document.createDocumentFragment();
    desc3.append(
        "Define Batch Job Settings."
    );
    new Setting(parent).setDesc(desc3);

    new Setting(parent)
        .setName('Ignored Tags')
        .setDesc('Separated with comma(,) with prefix(#).')
        .addText(text => text
            .setPlaceholder("#flomo,#cubox")
            .setValue(this.plugin.settings.ignoredTags)
            .onChange(async (value) => {
                  this.plugin.settings.ignoredTags = value;
                  await this.plugin.saveSettings();
                }
            ));
    new Setting(parent)
        .setName('Scan Range Path')
        .setDesc('Scan range path')
        .addText(text => text
            .setPlaceholder("1_Index/Diary")
            .setValue(this.plugin.settings.scanRangePath)
            .onChange(async (value) => {
                  this.plugin.settings.scanRangePath = value;
                  await this.plugin.saveSettings();
                }
            ));
    // .addText(text => text
    //     .setPlaceholder("1_Inbox/Diary")
    //     .setValue(this.plugin.settings.scanRangePath)
    //     .onChange(async (value) => {
    //             this.plugin.settings.scanRangePath = value;
    //             await this.plugin.saveSettings();
    //         }
    //     ))
    // .addText(text => text
    //     .setPlaceholder("1_Inbox/Diary/output.md")
    //     .setValue(this.plugin.settings.outputPath)
    //     .onChange(async (value) => {
    //             this.plugin.settings.outputPath = value;
    //             await this.plugin.saveSettings();
    //         }
    //     ))
    new Setting(parent)
        .setName('Output Path')
        .setDesc('Output path.')
        .addText(text => text
            .setPlaceholder("1_Index/Archive/tag-aggregations")
            .setValue(this.plugin.settings.outputPath)
            .onChange(async (value) => {
                  this.plugin.settings.outputPath = value;
                  await this.plugin.saveSettings();
                }
            ));
    new Setting(parent)
        .setName('Populate empty title')
        .setDesc('toggle to populate empty title.')
        .addToggle(toggle => toggle
            .setValue(this.plugin.settings.populateEmptyTitle)
            .onChange(async (value) => {
                  this.plugin.settings.populateEmptyTitle = value
                  await this.plugin.saveSettings()
                }
            ));
    new Setting(parent)
        .setName('Make title links or not')
        .setDesc('toggle to make title links.')
        .addToggle(toggle => toggle
            .setValue(this.plugin.settings.titleLinks)
            .onChange(async (value) => {
                  this.plugin.settings.titleLinks = value
                  await this.plugin.saveSettings()
                }
            ));
    /* end */
  }
}